<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{   
    //分类首页
    public function getIndex(Request $request)
    {
         //判断是否有查询条件
        if($request->input('keywords')){
            $cates = DB::table('cate')->where('tname','like','%'.$request->input('keywords').'%')->get();
        }else{
            $cates = self::getCates();
        }

        $list = $request->all();
        

        //解析模板
        return view('admin.cate.index',['cates'=>$cates,'list'=>$list]);
    }

    //分类添加
    public function getAdd()
    {
    	//查询所有
        $cates = self::getCates();

    	//解析模板
    	return view('admin.cate.add',['cates'=>$cates]);
    }

    //分类添加 数据处理
    public function postInsert(Request $request)
    {
    	// dd($request->all());
    	$data = $request->only('pid','tname','path');
    	//判断当前的分类是否为顶级分类
    	if($data['pid'] == 0){
    		$data['path'] = 0;
    	}else{
    		//查询父级的path路径
    		$res = DB::table('cate')->where('tid',$data['pid'])->first();
    		$data['path'] = $res->path.','.$data['pid'];
    	}

    	//上传数据库
    	$res = DB::table('cate')->insert($data);
    	if($res){
    		return redirect('/admin/cate/index')->with('success','分类添加成功');
    	}else{
    		return back()->with('error','分类添加失败');
    	}

    }

    //封装方法进行ajax删除
    public function postDelete(Request $request)
    {	
    	$tid = $request->input('tid');
    	$cates = DB::table('cate')->where('pid',$tid)->get();
    	$goods = DB::table('goods')->where('tid',$tid)->get();
    	    	
    		if(empty($cates) && empty($goods)){

    			$res = DB::table('cate')->where('tid',$tid)->delete();
    			echo 1;
    		}else{
                echo 0;
            }  		
    	 
    }

    //按照格式要求显示数据
    public static function getCates()
    {
    	$cates = DB::select("select *,concat(path,',',tid) as paths from cate order by paths");
    	// dd($cates);
    	foreach ($cates as $k=>$v)
    	{
    		$arr = explode(',',$v->path);
    		$len = count($arr)-1;
    		$cates[$k]->tname = str_repeat('|--->',$len).$v->tname;
    	}
    	return $cates;

    }


    // 封装方法进行修改显示

    public function getEdit(Request $request)
    {
    	//获取tid
    	$tid = $request->input('tid');
    	//获取单条数据
    	$cates = DB::table('cate')->where('tid',$tid)->first();
    	//查询父类tid
    	$cate = DB::table('cate')->where('tid',$cates->pid)->first();
    	//解析模板
    	return view('/admin/cate/edit',['cates'=>$cates,'cate'=>$cate]);
    }

    //封装方法进行数据修改

    public function postUpdate(Request $request)
    {
    	
    	$data = $request->only('tname');
    	//获取tid
    	$tid = $request->input('tid');
    	//修改
    	$res = DB::table('cate')->where('tid',$tid)->update($data);
    	if($res){
    		return redirect('/admin/cate/index')->with('success','分类修改成功');
    	} else{
    		return back()->with('error','分类修改失败');
    	}
    }


    // 根据pid获取当前类下的所有子分类 递归
    public static function getCatesByPid($pid)
    {
        // 获取pid下的所有子类
        $res = DB::table('cate')->where('pid',$pid)->get();

        $data = [];

        foreach ($res as $k => $v) {
            $v->sub = self::getCatesByPid($v->tid);
            $data[] = $v;
       }
    
             return $data;
    }
  


}
